/**
 * Created by szc on 2016/12/14.
 */
class AutoGameSys extends ChildPanle {
    private checkArr: any;
    private checkNumArr = [];
    private rotationModel = "any";
    private rotationArr = {};
    public constructor(context: any) {
        super(context);
        this.rotationArr = PreferenceData.getAutoPlayData();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
    protected onAddToStage() {
        new Bg(this, 'mask_in_png');
        var percentage = 0.7;
        // 加载主面板
        var imgCon = [];
        imgCon['url'] = 'auto_game_bg_png';
        imgCon['width'] = this.stage.stageWidth * percentage * 1;
        imgCon['height'] = this.stage.stageHeight * percentage;
        imgCon['y'] = this.stage.stageHeight * (1 - percentage) / 2;
        imgCon['x'] = (this.stage.stageWidth - imgCon['width']) / 2;
        this.addChild(new ImageBitmap(imgCon).getImgPoint());


        /**
         * 设置按钮
         */
        this.setNumpanle();
        var CheckBoxX = this.stage.stageWidth * 0.21;

        var checkBoxArr = [];
        this.checkArr = checkBoxArr;
        var checkBox_any = new CheckBox();
        checkBoxArr['any'] = checkBox_any;
        checkBox_any.x = CheckBoxX;
        checkBox_any.y = this.stage.stageHeight * 0.45;
        checkBox_any.name = "any";
        checkBox_any.touchEnabled = true;
        this.addChild(checkBox_any);
        if (this.rotationArr['any']) {
            checkBox_any.setOpen();
        }

        var checkBox_one = new CheckBox();
        checkBoxArr['one'] = checkBox_one;
        checkBox_one.x = CheckBoxX;
        checkBox_one.y = this.stage.stageHeight * 0.545;
        checkBox_one.name = "one";
        checkBox_one.touchEnabled = true;
        this.addChild(checkBox_one);
        if (this.rotationArr['one']) {
            checkBox_one.setOpen();
        }


        var checkBox_leftadd = new CheckBox();
        checkBoxArr['add'] = checkBox_leftadd;
        checkBox_leftadd.x = CheckBoxX;
        checkBox_leftadd.y = this.stage.stageHeight * 0.635;
        checkBox_leftadd.name = "add";
        checkBox_leftadd.touchEnabled = true;
        this.addChild(checkBox_leftadd);
        if (this.rotationArr['add']) {
            checkBox_leftadd.setOpen();
        }
        //
        //
        var checkBox_plus = new CheckBox();
        checkBoxArr['plus'] = checkBox_plus;
        checkBox_plus.x = CheckBoxX;
        checkBox_plus.y = this.stage.stageHeight * 0.725;
        checkBox_plus.name = "plus";
        checkBox_plus.touchEnabled = true;
        this.addChild(checkBox_plus);
        if (this.rotationArr['plus']) {
            checkBox_plus.setOpen();
        }


        //增加关闭按钮
        this.setClose();
        /**
         * 设置金额控制栏
         * @type {NumCtrl}
         */
        var xWidth = this.stage.stageWidth * 0.6;
        var oneMoney = new NumCtrl(this);
        this.checkNumArr['one'] = oneMoney;
        oneMoney.x = xWidth;
        oneMoney.y = this.stage.stageHeight * 0.54;
        this.addChild(oneMoney);
        if (this.rotationArr['one']) {
            oneMoney.setNum(this.rotationArr['one']);
        }

        var addMoney = new NumCtrl(this);
        this.checkNumArr['add'] = addMoney;
        addMoney.x = xWidth;
        addMoney.y = this.stage.stageHeight * 0.627;
        this.addChild(addMoney);

        if (this.rotationArr['add']) {
            addMoney.setNum(this.rotationArr['add']);
        }

        var plusMoney = new NumCtrl(this);
        this.checkNumArr['plus'] = plusMoney;
        plusMoney.x = xWidth;
        plusMoney.y = this.stage.stageHeight * 0.72;
        this.addChild(plusMoney);
        if (this.rotationArr['plus']) {
            plusMoney.setNum(this.rotationArr['plus']);
        }
        super.onAddToStage();

    }

    private onClose(evt: egret.TouchEvent) {
        var rotationArr = this.getRotationArr();
        /**
         * 获取转动模式
         */
        PreferenceData.setAutoPlayData(rotationArr);
        super.closeAm();
    }

    private setClose(): void {
        /**
         * 设置关闭按钮
         * @type {eui.Button}
         */
        var close_btn = new eui.Button();
        ///设置按钮的标签
        close_btn.skinName = "close_buttom";
        close_btn.width = 60;
        close_btn.height = close_btn.width;
        close_btn.x = this.stage.stageWidth * 0.81;
        close_btn.y = this.stage.stageHeight * 0.12;
        this.addChild(close_btn);
        close_btn.touchEnabled = true;
        close_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
    }


    private setNumpanle(): void {

        var numBtuGroup = new egret.DisplayObjectContainer();

        var numBtuArr = ['m_rotation_10', 'm_rotation_30', 'm_rotation_50', 'm_rotation_99'];

        var numBtuNumArr = [10, 30, 50, 99];  //可以旋转的次数

        for (var i = 0; i < numBtuArr.length; i++) {
            var _numBtu = createNumBtu.call(this, numBtuArr[i]);
            _numBtu.x = i * 0.155 * this.stage.stageWidth;
            _numBtu['num'] = numBtuNumArr[i];
            _numBtu.touchEnabled = true;
            _numBtu.addEventListener(egret.TouchEvent.TOUCH_TAP, this.setRotationNum, this);
            numBtuGroup.addChild(_numBtu);
        }
        /**
         * 将按钮加入目前布局类中
         */
        numBtuGroup.x = this.stage.stageWidth * 0.21;
        numBtuGroup.y = this.stage.stageHeight * 0.3;
        this.addChild(numBtuGroup);

        function createNumBtu(str) {
            var _btn = new eui.Button();
            ///设置按钮的标签
            _btn.skinName = str;
            _btn.width = 120;
            _btn.height = 50;
            _btn.touchEnabled = true;
            return _btn;
        }
    }


    /**
     * 设置需要旋转的次数
     */
    private setRotationNum(evt: egret.TouchEvent): void {
        var num = evt.target.num;
        this.closeAm();
        var point = this;
        /**
         * 获取自动旋转的相关的参数
         */
        var rotationArr = this.getRotationArr();
        /**
         * 获取转动模式
         */
        PreferenceData.setAutoPlayData(rotationArr);
        window.setTimeout(point.context.gunDeal.gunSetNumRotate(num, rotationArr), 1000);
    }

    private getRotationArr() {
        var rotationArr = this.rotationArr;
        rotationArr["any"] = false;
        rotationArr["one"] = false;
        rotationArr["plus"] = false;
        rotationArr["add"] = false;

        if (this.checkArr["any"].signal == 1) {
            rotationArr["any"] = true;
        }
        if (this.checkArr["one"].signal == 1) {
            rotationArr["one"] = this.checkNumArr["one"].getNum();
        }
        if (this.checkArr["plus"].signal == 1) {
            rotationArr["plus"] = this.checkNumArr["plus"].getNum();
        }
        if (this.checkArr["add"].signal == 1) {
            rotationArr["add"] = this.checkNumArr["add"].getNum();
        }
        return rotationArr;
    }
}